package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.Menu;
import com.wuming.caiwu.service.MenuService;
import com.wuming.caiwu.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/manageMenu", method = RequestMethod.GET)
    public String manageMenu() {
        return "/cms/manageMenu";
    }

    @RequestMapping(value = "/addMenu", method = RequestMethod.GET)
    public String addMenu() {
        return "/cms/editMenu";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String menuName,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<Menu> students = menuService.getMenusByPage(menuName, pageIndex, pageSize);
            int total = menuService.getTotal(menuName);
            return CommonResult.createSuccessResult(students, total, "查询菜单信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询菜单信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询菜单信息失败.");
        }
    }

    @RequestMapping(value = "/editMenu/{id}", method = RequestMethod.GET)
    public String editStudent(Model model, @PathVariable Long id) {
        logger.info("准备修改菜单信息ID:{}", id);
        Menu product = menuService.getMenuById(id);
        model.addAttribute("menu", product);
        return "/cms/editMenu";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(Menu product) {
        try {
            if (null != product.getId()) {
                menuService.modifyMenu(product);
            } else {
                menuService.saveMenu(product);
            }
        } catch (Exception e) {
            logger.error("保存菜单信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存菜单基本信息失败", false);
        }
        return new JsonResult<Object>(product.getId(), "保存菜单基本信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        logger.info("准备删除角色信息ID:{}", id);
        try {
            menuService.removeMenuById(id);
            return CommonResult.createSuccessResult("删除角色信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除角色信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除角色信息失败.");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public JsonResult uploadImg(MultipartFile file) {
        String imageUrl = OssUtil.upload(file, OssUtil.FileDirType.ARTICLE_IMAGES);;
        return new JsonResult<Object>(imageUrl, "上传图片信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    public JsonResult uploadVideo(MultipartFile file) {
        String videoUrl = OssUtil.upload(file, OssUtil.FileDirType.ARTICLE_VIDEO);;
        return new JsonResult<Object>(videoUrl, "上传视频信息成功.", true);
    }

}
